package top.lyc829.zzl_android.ui.main;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.MsgNumberBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class MainViewModel extends ViewModel {

    private MutableLiveData<MsgNumberBean> msgNumberLiveData;

    public MutableLiveData<MsgNumberBean> getMsgNumberLiveData() {
        if (msgNumberLiveData == null) {
            msgNumberLiveData = new MutableLiveData<>();
        }
        return msgNumberLiveData;
    }

    public void updateMsgNumber() {
        Network.getMsgNumber(new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {

                String resStr = res.body().string();

                ResultBean<MsgNumberBean> resultBean = JsonUtilNew.fromJsonObject(resStr, MsgNumberBean.class);

                if (resultBean.getCode().equals(200)) {
                    getMsgNumberLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }


}
